//
//  CommodityView.m
//  used
//
//  Created by lanou on 16/7/19.
//  Copyright © 2016年 王凯. All rights reserved.
//

#import "CommodityView.h"

#import <CDChatManager.h>
#import "ChatViewController.h"

@interface CommodityView()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *nameLeftdistance;

@property (weak, nonatomic) IBOutlet UICollectionView *CommodityCollectionVeiw;  //商品图片

@property (weak, nonatomic) IBOutlet UIImageView *userimge; //头像

@property (weak, nonatomic) IBOutlet UILabel *sallername; //昵称

@property (weak, nonatomic) IBOutlet UILabel *saleprice; //现价

@property (weak, nonatomic) IBOutlet UILabel *desc; //商品信息

@property (weak, nonatomic) IBOutlet UILabel *city; //城市名字

@property (weak, nonatomic) IBOutlet UILabel *area; //城市区域


@property (nonatomic, strong)NSArray *collectionArray;

@property (nonatomic, strong)UIImageView *currImage; //存放当前点击照片

@property (nonatomic,weak) UILabel *indexLabel;

@property (nonatomic, strong)MBProgressHUD *hud;


@end

@implementation CommodityView

//懒加载
- (MBProgressHUD *)hud {
    
    if (!_hud) {
        _hud = [[MBProgressHUD alloc]initWithView:self];
        [self addSubview:_hud];
    }
    return _hud;
}

- (UIImageView *)currImage {

    if (!_currImage) {
        _currImage = [[UIImageView alloc] init];
    }

    return _currImage;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.CommodityCollectionVeiw.dataSource = self;
    self.CommodityCollectionVeiw.delegate = self;
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];

    layout.itemSize = CGSizeMake(kWidth - 40, self.CommodityCollectionVeiw.height);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.CommodityCollectionVeiw.collectionViewLayout = layout;
    
    [self.CommodityCollectionVeiw registerNib:[UINib nibWithNibName:@"CollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"CollectionViewCell_cell"];
   
}

- (IBAction)clickIcon:(id)sender {
    
    if ([self.delegate respondsToSelector:@selector(clictIconMessageWithUserid:)]) {
        [self.delegate clictIconMessageWithUserid:self.commodityModel.userid];
    }
    
}

- (void)setCommodityModel:(KLCommodity *)commodityModel {
    
    _commodityModel = commodityModel;
    self.saleprice.text = commodityModel.saleprice.description; //现价
    if (commodityModel.userimage != nil) {
        [self.userimge sd_setImageWithURL:[NSURL URLWithString:commodityModel.userimage]]; //头像
    }

    self.sallername.text = commodityModel.sallername; //昵称
    self.city.text = commodityModel.city; //城市
    self.area.text = commodityModel.area; //区域
    self.desc.text = commodityModel.desc; //商品信息
    
    self.collectionArray = commodityModel.imageurl;
    [self.CommodityCollectionVeiw reloadData];
    
    BmobObject *readCount = [BmobObject objectWithClassName:@"ReadCount"];
    [readCount setObject:self.commodityModel.productname forKey:@"productname"];
    [readCount setObject:self.commodityModel.desc forKey:@"desc"];
    

    [readCount saveInBackground];
    
    
}




#pragma mark -collectionView

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.collectionArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionViewCell_cell" forIndexPath:indexPath];
    //设置cell
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:self.collectionArray[indexPath.row]]];
    self.currImage = cell.imageView;
    return cell;
    
}

//点击缩放照片
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

//    [ZoomImage showImage:self.currImage];
    [ZoomImage showImage:self.currImage imageArray:self.collectionArray];
}


//聊天按钮
- (IBAction)chatButton:(UIButton *)sender {
    
    
    BmobUser *user = [BmobUser getCurrentUser];
    if (!user) {
        YSLLoginRegisterViewController *login = [[YSLLoginRegisterViewController alloc] init];
        [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:login animated:YES completion:nil];
    }else {
        
        NSString *client = [user objectForKey:@"uname"]?[user objectForKey:@"uname"]:user.username;
        
        [[CDChatManager manager]openWithClientId:client callback:^(BOOL succeeded, NSError *error) {

            if (self.sallername.text > 0) {
                
                [[CDChatManager manager] fetchConversationWithOtherId:self.sallername.text callback:^(AVIMConversation *conversation, NSError *error) {

                    if (conversation) {
                        
                        ChatRoomViewController *chatRoomVC = [[ChatRoomViewController alloc] initWithConversation:conversation];
                        [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController: [[UINavigationController alloc] initWithRootViewController:chatRoomVC] animated:YES completion:nil];
                        //传昵称
                        chatRoomVC.name = self.sallername.text;
                    }
                }];
            }
        }];
        
    }
}


//点赞
- (IBAction)praiseButton:(UIButton *)sender {

    if (![BmobUser getCurrentUser]) {
        YSLLoginRegisterViewController *login = [[YSLLoginRegisterViewController alloc] init];
        [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:login animated:YES completion:nil];
    }else {
        BmobQuery *query = [BmobQuery queryWithClassName:@"Zan"];
        [query whereKey:@"user" equalTo:[BmobUser getCurrentUser]];
        [query whereKey:@"desc" equalTo:self.commodityModel.desc];
        [query findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
            
            if (array.count) {
                [self showMessage:@"您已经点过赞了"];
            }else {
                BmobObject *zan = [BmobObject objectWithClassName:@"Zan"];
                
                [zan setObject:[BmobUser getCurrentUser] forKey:@"user"];
                //    [zan setObject:self.commodityModel.sallername forKey:@"sallername"];
                [zan setObject:self.commodityModel.productname forKey:@"productname"];
                [zan setObject:self.commodityModel.desc forKey:@"desc"];
                [zan setObject:self.commodityModel.imageurl[0] forKey:@"image"];
                [zan setObject:self.commodityModel.saleprice forKey:@"saleprice"];
                //    [zan setObject:self.commodityModel.userimage forKey:@"userIcon"];
                [zan setObject:self.ID forKey:@"productID"];
                [zan saveInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
                    if (isSuccessful) {
                        [self showMessage:@"点了一个赞"];
                    }
                }];
            }
            
        }];
    
    }
}

//想要购买
- (IBAction)buyButton:(UIButton *)sender {
    
//    YSLBuyViewController *buyVC = [[YSLBuyViewController alloc] initWithNibName:@"YSLBuyViewController" bundle:nil];
    
    YSLBuyViewController *buyVC = [[YSLBuyViewController alloc] init];
    
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController: [[UINavigationController alloc] initWithRootViewController:buyVC] animated:YES completion:nil];
    buyVC.pric = self.saleprice.text;
    buyVC.salepric = self.saleprice.text;
    [buyVC.imageView sd_setImageWithURL:[NSURL URLWithString:self.collectionArray[0]]];

    buyVC.des = self.desc.text;
    buyVC.cit = self.city.text;
    buyVC.are = self.area.text;
    
}

- (void)showMessage:(NSString *)message {
    self.hud.labelText = message;
    self.hud.mode = MBProgressHUDModeText;
    [self.hud showAnimated:YES whileExecutingBlock:^{
        sleep(1);
    } completionBlock:^{
        [self.hud removeFromSuperViewOnHide];
        self.hud = nil;
    }];
    
}

@end
